#pragma once
#include <Windows.h>
#include <iostream>

// How to use:
// Example 1:
// double d1 = Tick();
// double d2 = Tick();
// double time = d2 - d1;
//
// Example 2:
// {
//   CAutoTimer at;
//   // ...
// }


// this is a internal function
static LONGLONG GetPerformanceFrequency()
{
	LARGE_INTEGER qpFrequency;
	::QueryPerformanceFrequency(&qpFrequency);
	return qpFrequency.QuadPart;
}


namespace lz
{

double Tick()
{
	static LONGLONG llFrequency = GetPerformanceFrequency();
	LARGE_INTEGER qpCounter;
	::QueryPerformanceCounter(&qpCounter);

	return qpCounter.QuadPart / (double)llFrequency;
}

class CAutoTimer
{
public:
	CAutoTimer()
	{
		start_ = Tick();

	}

	~CAutoTimer()
	{
		end_ = Tick();

		Output();
	}

	inline double GetElapsedTime() const {return end_ - start_;}

	inline void Output()
	{
		char output[100];
		sprintf(output, "Elapsed Time: %f\n", GetElapsedTime());
		std::cout << output << std::endl;
	}

private:
	double start_;
	double end_;
};

}